package com.meixiang.view;

import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;

/**
 * 横向可拖动的ScrollView
 * Created by xyj on 2016/6/1.
 */
public class ColumnHorizontalScrollView extends HorizontalScrollView {
    /**
     * 整体布局
     */
    private View ll_content ;
    /**
     * 更多栏目布局
     */
    private View ll_more ;
    /**
     * 拖动的视图
     */
    private View rl_column ;
    /** 右阴影图片*/
    private ImageView rightImage ;
    /**
     * 屏幕的宽度
     */
    private int mScreenWidth =0;
    /** 父类的活动activity */
    private Activity activity;

    public ColumnHorizontalScrollView(Context context) {
        super(context);
    }

    public ColumnHorizontalScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public ColumnHorizontalScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    /**
     * 拖动的时候执行
     */
    @Override
    protected void onScrollChanged(int paramInt1, int paramInt2, int paramInt3, int paramInt4) {
        super.onScrollChanged(paramInt1, paramInt2, paramInt3, paramInt4);
        shade_ShowOrHide();
        if(!activity.isFinishing() && ll_content !=null && rightImage!=null && ll_more!=null && rl_column !=null){
            if(ll_content.getWidth() <= mScreenWidth){
                rightImage.setVisibility(View.GONE);
            }
        }else{
            return;
        }
        if(paramInt1 ==0){
            rightImage.setVisibility(View.VISIBLE);
            return;
        }
        if(ll_content.getWidth() - paramInt1 + ll_more.getWidth() + rl_column.getLeft() == mScreenWidth){
            rightImage.setVisibility(View.GONE);
            return;
        }
        rightImage.setVisibility(View.VISIBLE);
    }

    /***
     * 传入父类布局中的资源文件
     * @param activity
     * @param mScreenWidth 屏幕的宽度
     * @param paramView1 Item外层父类布局
     * @param paramView3 右侧阴影视图
     * @param paramView4 右侧搜索视图
     * @param paramView5 最外层视图
     **/
    public void setParam(Activity activity, int mScreenWidth,View paramView1, ImageView paramView3 ,View paramView4,View paramView5){
        this.activity = activity;
        this.mScreenWidth = mScreenWidth;
        ll_content = paramView1;
        rightImage = paramView3;
        ll_more = paramView4;
        rl_column = paramView5;
    }
    /**
     * 判断左右阴影的显示隐藏效果
     * */
    public void shade_ShowOrHide() {
        if (!activity.isFinishing() && ll_content != null) {
            measure(0, 0);
            //如果整体宽度小于屏幕宽度的话，那左右阴影都隐藏
            if (mScreenWidth >= getMeasuredWidth()) {
                rightImage.setVisibility(View.GONE);
            }
        } else {
            return;
        }
        //如果滑动在最左边时候，左边阴影隐藏，右边显示
        if (getLeft() == 0) {
            rightImage.setVisibility(View.VISIBLE);
            return;
        }
        //否则，说明在中间位置，右阴影都显示
        rightImage.setVisibility(View.VISIBLE);
    }
}
